package com.wangyadong.hobby.thread;

public class CheckingAccount {
    private int balance;

    public CheckingAccount(int initialBalance) {
        balance = initialBalance;
    }

    //check then act
    public boolean withdraw(int amount) {
        synchronized (this) {
            boolean b = amount <= balance;
            if (b) {
                try {
                    Thread.sleep((int) (Math.random() * 200));
                } catch (InterruptedException ie) {
                }
                balance -= amount;
                return true;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        final CheckingAccount ca = new CheckingAccount(100);
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                for (int i = 0; i < 10; i++)
                    System.out.println(name + " withdraws $10:" + ca.withdraw(10));
            }
        };
        Thread thdHusband = new Thread(r);
        thdHusband.setName("Husband");
        Thread thdWife = new Thread(r);
        thdWife.setName("Wife");
        thdHusband.start();
        thdWife.start();
    }
}